package com.yugao.fintech.antelope.system;

import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.yugao.fintech.antelope.base.model.enums.ConfigDefineEnum;
import com.yugao.fintech.antelope.base.model.enums.ConfigKeyEnum;
import com.yugao.fintech.antelope.base.model.module.auth.LoginUser;
import com.yugao.fintech.antelope.base.model.module.config.ConfigInfo;
import com.yugao.fintech.antelope.system.model.entity.SysUser;
import com.yugao.fintech.antelope.system.model.rqrs.accesskey.AccessKeyResp;
import com.yugao.fintech.antelope.system.model.rqrs.config.ConfigResp;
import com.yugao.fintech.antelope.system.model.rqrs.log.LoginUserGetReq;
import com.yugao.fintech.antelope.system.model.rqrs.log.OperateLogSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.tenant.TenantSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.tenant.TenantUpdateReq;
import com.yugao.fintech.antelope.system.model.rqrs.user.LoginUserDeleteReq;
import com.yugao.fintech.antelope.system.model.rqrs.user.UserPageReq;
import com.yugao.fintech.antelope.system.model.rqrs.user.UserResp;
import com.yugao.fintech.antelope.system.model.rqrs.user.UserSaveReq;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.core.response.RetOps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Lazy
@Service
@ConditionalOnClass({FeignAutoConfiguration.class, NacosServiceAutoConfiguration.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class SystemApiCloudImpl implements SystemApi {
    @Autowired
    private RemoteSystemService service;

    @Override
    public AccessKeyResp getAccessKeyBySecretId(String secretId) {
        Resp<AccessKeyResp> rs = service.getAccessKeyBySecretId(secretId);
        RetOps.of(rs).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return rs.getData();
    }

    @Override
    public AccessKeyResp getAccessKeyById(Long id) {
        Resp<AccessKeyResp> rs = service.getAccessKeyById(id);
        RetOps.of(rs).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return rs.getData();
    }

    @Override
    public void saveOperateLog(OperateLogSaveReq rq) {
        service.saveOperateLog(rq);
    }


    @Override
    public LoginUser getLoginUser(LoginUserGetReq req) {
        Resp<LoginUser> resp = service.getLoginUser(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public Long saveUser(UserSaveReq req) {
        Resp<Long> resp = service.saveUser(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<UserResp> listUser(UserPageReq query) {
        Resp<List<UserResp>> resp = service.listUser(query);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<UserResp> listUserByIds(List<Long> userIds) {
        Resp<List<UserResp>> resp = service.listUserByIds(userIds);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void removeUserById(Long userId) {
        Resp<?> resp = service.removeUserById(userId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public UserResp getUserById(Long userId) {
        Resp<UserResp> resp = service.getUserById(userId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteLoginUser(LoginUserDeleteReq req) {
        Resp<?> resp = service.deleteLoginUser(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public Long saveTenant(TenantSaveReq req) {
        Resp<Long> resp = service.saveTenant(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteTenant(Long tenantId) {
        Resp<?> resp = service.deleteTenant(tenantId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public void updateTenant(TenantUpdateReq req) {
        Resp<?> resp = service.updateTenant(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public ConfigResp getConfig(ConfigKeyEnum configKey) {
        throw new UnsupportedOperationException("尚未实现远程调用");
    }

    @Override
    public ConfigInfo getUseConfig(ConfigDefineEnum configDefine) {
        Resp<ConfigInfo> resp = service.getUseConfig(configDefine.name());
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public Map<String, List<String>> listAssignRoleUrl(List<Long> roleIds) {
        Resp<Map<String, List<String>>> resp = service.listAssignRoleUrl(roleIds);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public SysUser getUserByAccount(String account) {
        Resp<SysUser> resp = service.getUserByAccount(account);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }
}
